package com.xmtlxx.oms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.oms.domain.bo.BoxDeptBo;
import com.xmtlxx.oms.domain.vo.BoxDeptVo;
import com.xmtlxx.oms.service.IBoxDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 箱子领用Controller
 *
 * @author xmtlxx
 * @date 2022-12-07
 */
@Validated
@Api(value = "箱子领用控制器", tags = {"箱子领用管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/oms/boxDept")
public class BoxDeptController extends BaseController {

    private final IBoxDeptService iBoxDeptService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<BoxDeptVo> initBean(){
        BoxDeptVo vo = new BoxDeptVo();
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询箱子领用列表
     */
    @ApiOperation("查询箱子领用列表")
    @SaCheckPermission("oms:boxDept:list")
    @GetMapping("/list")
    public TableDataInfo<BoxDeptVo> list(@Validated(QueryGroup.class) BoxDeptBo bo, PageQuery pageQuery) {
        return iBoxDeptService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出箱子领用列表
     */
    @ApiOperation("导出箱子领用列表")
    @SaCheckPermission("oms:boxDept:export")
    @Log(title = "箱子领用", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated BoxDeptBo bo, HttpServletResponse response) {
        List<BoxDeptVo> list = iBoxDeptService.queryList(bo);
        ExcelUtil.exportExcel(list, "箱子领用", BoxDeptVo.class, response);
    }

    /**
     * 获取箱子领用详细信息
     */
    @ApiOperation("获取箱子领用详细信息")
    @SaCheckPermission("oms:boxDept:query")
    @GetMapping("/{id}")
    public R<BoxDeptVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        return R.ok(iBoxDeptService.queryById(id));
    }

    /**
     * 新增箱子领用
     */
    @ApiOperation("新增箱子领用")
    @SaCheckPermission("oms:boxDept:add")
    @Log(title = "箱子领用", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BoxDeptVo vo) {
        BoxDeptBo bo = new BoxDeptBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iBoxDeptService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改箱子领用
     */
    @ApiOperation("修改箱子领用")
    @SaCheckPermission("oms:boxDept:edit")
    @Log(title = "箱子领用", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BoxDeptVo vo) {
        BoxDeptBo bo = new BoxDeptBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iBoxDeptService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除箱子领用
     */
    @ApiOperation("删除箱子领用")
    @SaCheckPermission("oms:boxDept:remove")
    @Log(title = "箱子领用", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iBoxDeptService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
